JBoss Community Archive (Read Only)

XNIO

Channel Listeners

The application program is notified of events on a channel by way of the ChannelListener interface. A class which implements this interface is known as a channel listener. The interface's single method, handleEvent(), is invoked when a specific event occurs on a channel.

By default, XNIO uses only a small number of dedicated threads to handle events. This means that in general, channel listeners are expected to run only for a brief period of time (in other words, the listener should be non-blocking). If a channel listener runs for an extended period of time, other pending listeners will be starved while the XNIO provider waits for the listener to complete, causing major performance degradation and possibly even deadlocks. If your design calls for long-running listeners, then such a listener should be implemented asynchronously using the XNIO worker thread pool (see Workers). Such a configuration can simplify your design, at the cost of a slight increase in latency.

Registering a channel listener involves accessing the setter for the corresponding listener type. The setter can accept a listener, which will be stored internally, replacing any previous value, to be invoked when the listener's condition is met. The new listener value will take effect immediately. Setting a listener to null will cause the corresponding event to be ignored. By default, unless explicitly specified otherwise, all listeners for a channel will default to null and have to be set in order to receive the corresponding notification.

The ChannelListener interface has a type parameter which specifies what channel type the listener expects to receive. Every channel listener setter will accept a channel listener for the channel type with which it is associated; however, they will additionally accept a channel listener for any supertype of that channel type as well. This allows general-purpose listeners to be applied to multiple channel types, while also allowing special-purpose listeners which take advantage of the features of a more specific channel type.

There are several types of events for which a channel listener may be registered. Though the circumstances for each type may differ, the same interface is used for all of them. The types are:

Not all event types are relevant for all channels, however most channel types will support notification for channel close events, and most channel types will have a way to register a listener for channel binding or opening, though the mechanism may vary depending on whether the channel in question is a client or server, TCP or UDP, etc.

JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-13 13:59:27 UTC, last content change 2012-07-02 21:36:07 UTC.